#!/usr/bin/env python
# -*- coding:utf-8 -*-
import requests
import datetime
import time
import json
from bs4 import BeautifulSoup

session = requests.session()
host = 'https://www.1393p.com'
url2 = host + '/pk10/getawarddata?r=' + str(time.time())
hezhi = 0  # 当前和值

# 新线路配置
host2 = 'https://www.cp95558.com'
today_str = str(datetime.date.today().strftime('%Y-%m-%d'))
url3 = host2 + '/data/bjpk10/lotteryList/' + today_str + '.json?' + str(time.time())
hezhi_line2 = 0  # 线路2 当前和值


# 取开奖结果
def get_awardres(res):
    result = {}
    if res is None:
        return result
    dict_sum2 = json.loads(res)
    result2 = dict_sum2.get('current').get('result')
    result['awardinfo'] = result2  # 开奖结果
    result['awardTime'] = dict_sum2.get('current').get('awardTime')  # 开奖时间
    result['awardperiod'] = dict_sum2.get('current').get('period')  # 开奖期号
    resarr = result2.split(',')
    hezhi = int(resarr[0]) + int(resarr[1])
    result['awardhezhi'] = hezhi
    return result


# 取开奖结果-线路2
def get_awardres_line2(res):
    result = {}
    if res is None:
        return result
    dict_sum2 = json.loads(res)
    openstr = dict_sum2[0]['openNum']  # 开奖结果
    result['awardTime'] = dict_sum2[0]['openDateTime']  # 开奖时间
    result['awardperiod'] = dict_sum2[0]['issue']  # 开奖期号
    result['awardinfo'] = str(openstr[0]) + ',' + str(openstr[1]) + ',' + str(openstr[2]) + ',' + str(
        openstr[3]) + ',' + str(
        openstr[4]) + ',' + str(openstr[5]) + ',' + str(openstr[6]) + ',' + str(openstr[7]) + ',' + str(
        openstr[8]) + ',' + str(openstr[9])
    hezhi_line2 = int(openstr[0]) + int(openstr[1])
    result['awardhezhi'] = hezhi_line2
    return result


try:
    # 获取当前开奖-线路1
    resp2 = session.get(url2)
    awards = get_awardres(resp2.text)
    print awards
    # 获取当前开奖-线路2
    resp3 = session.get(url3)
    print resp3.text
    soup = BeautifulSoup(resp3.text, 'html.parser')
    result = soup.find("meta", attrs={"http-equiv": "refresh"})
    url = ''
    if result:
        wait, text = result["content"].split(";")
        if text.strip().lower().startswith("url="):
            url = text[4:]
            print url
    url3 = host2 + url
    print url3
    resp3 = session.get(url3)
    print resp3.text
    awards2 = get_awardres_line2(resp3.text)

    currentAward = None
    currentAwardFlag = 0

    if awards2:
        period02 = int(awards2['awardperiod'])
    else:
        period02 = 0

    if awards:
        period01 = int(awards['awardperiod'])
    else:
        period01 = 0

    if period01 > period02:
        currentAwardFlag = 0
    else:
        currentAwardFlag = 1

    if currentAwardFlag == 0 and awards:
        currentAward = awards

    if currentAwardFlag == 1 and awards2:
        currentAward = awards2

    if currentAward:
        print currentAward
    else:
        exit()

        # # 初始化redis
        # pwd = ""  # redis秘钥
        # r = redis.Redis(host='127.0.0.1', port=6379, db=0, password=pwd)
        # awardperiod = int(r.get('awardperiod'))
        # awardinfo = r.get('awardinfo')
        # nowadward2 = int(currentAward['awardperiod'])  # 当前开奖期号
        # upawards = 0  # 更新redis 开奖结果标识
        #
        # # 更新开奖数据
        # if awardperiod is None and currentAward:
        #     upawards = 1
        # else:
        #     if nowadward2 > awardperiod and currentAward:
        #         upawards = 1
        #
        # if upawards == 1:
        #     r.set('awardperiod', currentAward['awardperiod'])
        #     r.set('awardinfo', currentAward['awardinfo'])
        #     r.set('awardTime', currentAward['awardTime'])
        #     r.set('awardhezhi', currentAward['awardhezhi'])
        #
        # print upawards


except Exception as e:
    pass
